<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\Curl;
class Spider extends Base{
    public function spider(){
        header( "Content-type:text/html;Charset=utf-8" );
        $spiderarr=array();
        for($i=1;$i<21;$i++){
            $ch = curl_init();
            $url ="http://tool.110.com/fy.php?p=".$i."&q=&province=0&city=0";
            curl_setopt ( $ch , CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" );
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $content=curl_exec($ch);
            preg_match_all("/<span class=\"contentd\">(.*?)<\/span>/",$content,$matchs);
            foreach ($matchs[1] as $key => $value) {
                array_push($spiderarr,$value);
            }
        }
        // Db::name('test')->insert(['test'=>json_encode($spiderarr)]);
        for($j=0;$j<count($spiderarr);$j++){
            echo $spiderarr[$j];
            echo '<br>';
        }
    }
    public function moneyapi(){
        // $moneyapi = Curl::http_curl("http://hq.sinajs.cn/list=sz300001");
        // var_dump($moneyapi);die;
        // var_dump(explode(',',$moneyapi));die;
        // return json(['code' => 201, 'data' =>$moneyapi]);exit();
        return $this->fetch();
    }
    public function stock(){
        $spiderarr=array();
        header( "Content-type:text/html;Charset=utf-8" );
        $ch = curl_init();
        $url ="http://www.txsec.com/inc1/gpdm.asp";
        curl_setopt ( $ch , CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" );
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content=curl_exec($ch);
        preg_match_all("/<td width=\"100\">(.*?)<\/td>/",$content,$matchs);
        foreach ($matchs[1] as $key => $value) {
            array_push($spiderarr,$value);
        }
        $stockarr=array();
        for($j=0;$j<count($spiderarr);$j++){
            $spiderarr[$j]=str_replace(' ','',$spiderarr[$j]);
            if (is_numeric($spiderarr[$j])) {
                array_push($stockarr,$spiderarr[$j]);
            }
        }
        $stockarr=array_unique($stockarr);
        return json(['code' => 201, 'data' =>$stockarr]);exit();

    }
    public function stocks(){
        $spiderarr=array();
        header( "Content-type:text/html;Charset=utf-8" );
        $ch = curl_init();
        $url ="http://www.bestopview.com/stocklist.html";
        curl_setopt ( $ch , CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36" );
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $content=curl_exec($ch);
        preg_match_all("/<a href=\"http:\/\/www.bestopview.com\/superView.php\?stockcode=(.*?)\" target=\"_blank\">/",$content,$matchs);
        return json(['code' => 201, 'data' =>$matchs[1]]);exit();

    }
    public function eaststock(){
        $eaststockarr=array();
        //http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?cb=jQuery1124023371453919335106_1557908714975&type=CT&token=4f1862fc3b5e77c150a2b985b12db0fd&sty=FCOIATC&js=(%7Bdata%3A%5B(x)%5D%2CrecordsFiltered%3A(tot)%7D)&cmd=C._A&st=(ChangePercent)&sr=-1&p=182&ps=20&_=1557908715534
        for ($t=1; $t <182 ; $t++) { 
            $results=Curl::http_curl("http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?cb=jQuery1124023371453919335106_1557908714975&type=CT&token=4f1862fc3b5e77c150a2b985b12db0fd&sty=FCOIATC&js=(%7Bdata%3A%5B(x)%5D%2CrecordsFiltered%3A(tot)%7D)&cmd=C._A&st=(ChangePercent)&sr=-1&p={$t}&ps=20&_=1557908715534");
            $results=str_replace('jQuery1124023371453919335106_1557908714975({data:','',$results);
            $results=str_replace(',recordsFiltered:3636})','',$results);
            $results=json_decode($results);
            for ($i=0; $i <count($results) ; $i++) { 
                $tmd=explode(',',$results[$i]);
                array_push($eaststockarr,$tmd[1]);
            }
        }
        // Db::name('test')->insert(['test'=>json_encode($eaststockarr)]);
        
    }
    public function shenjian(){
        $starttime=date('Y-m-d',time()-7*86400);
        $endtime=date('Y-m-d',time());
        $url='https://api.shenjian.io/';
        $parm['appid']='49eee0d8c17163737d5e1a8e0fcfd0a8';
        $parm['code']='000001';
        $parm['index']='false';
        $parm['k_type']='day';
        $parm['fq_type']='qfq';
        $parm['start_date']=$starttime;
        $parm['end_start']=$endtime;
        $results=Curl::https_curl($url,$parm);
        return $results;
    }
    public function findstock(){
        $stocks=Db::name('test')->where('id',3)->find();
        $stockarr=json_decode($stocks['test']);
        return json(['code' => 201, 'data' =>$stockarr]);exit();
    }
    public function nowstock(){
        $stocks=Db::name('test')->where('id',3)->find();
        $stockarr=json_decode($stocks['test']);
        $nowstock=array();
        $starttime=date('Y-m-d',time()-7*86400);
        $endtime=date('Y-m-d',time());
        $url='https://api.shenjian.io/';
        $parm['appid']='49eee0d8c17163737d5e1a8e0fcfd0a8';
        // $parm['code']='000001';
        $parm['index']='false';
        $parm['k_type']='day';
        $parm['fq_type']='qfq';
        $parm['start_date']=$starttime;
        $parm['end_start']=$endtime;
        for ($a=0; $a <100 ; $a++) { //建议每次查询50个
            $parm['code']=$stockarr[$a];
            $results=Curl::https_curl($url,$parm);
            $tmd=json_decode($results);
            if($tmd->error_code==0){
                if (is_array($tmd->data)) {
                    if (count($tmd->data)>=2) {
                        $ii=count($tmd->data)-1;
                        $ij=count($tmd->data)-2;
                        if ($tmd->data[$ii]->close>$tmd->data[$ii]->open) {
                            if ($tmd->data[$ii]->close>$tmd->data[$ij]->close) {
                                if ($tmd->data[$ij]->close<$tmd->data[$ij]->open) {
                                    $round=$tmd->data[$ii]->close-$tmd->data[$ii]->open;
                                    $red=$round/$tmd->data[$ii]->open;
                                    if ($red>0.025) {
                                        array_push($nowstock,$parm['code']);
                                    }
                                }
                            }
                        }
                    }
                }
                
            }
        }
        return json(['code' => 201, 'data' =>$nowstock]);exit();
    }
}